/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.mes.quality.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.modules.mes.quality.entity.AnalysisAssemblyOffline;
import org.springblade.modules.mes.quality.entity.AnalysisBarcodeChange;
import org.springblade.modules.mes.quality.mapper.AnalysisAssemblyOfflineMapper;
import org.springblade.modules.mes.quality.service.IAnalysisAssemblyOfflineService;
import org.springblade.modules.mes.quality.vo.AnalysisAssemblyOfflineVO;
import org.springblade.modules.mes.quality.vo.AssemblyNoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 质量分析-总成下线数据 服务实现类
 *
 * @author liuchao
 * @since 2023-02-20
 */
@Service
//@DS(value = "slave")
public class AnalysisAssemblyOfflineServiceImpl extends BaseServiceImpl<AnalysisAssemblyOfflineMapper, AnalysisAssemblyOffline> implements IAnalysisAssemblyOfflineService {

    @Override
    public IPage<AnalysisAssemblyOfflineVO> selectAnalysisAssemblyOfflinePage(IPage<AnalysisAssemblyOfflineVO> page, AnalysisAssemblyOfflineVO analysisAssemblyOffline) {
        return page.setRecords(baseMapper.selectAnalysisAssemblyOfflinePage(page, analysisAssemblyOffline));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateNew(List<AnalysisBarcodeChange> analysisBarcodeChangeList) {
        return baseMapper.updateNew(analysisBarcodeChangeList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void million() {
        System.out.println("===== 开始插入数据 =====");
        long startTime = System.currentTimeMillis();
        try {
            List<AnalysisAssemblyOffline> analysisAssemblyOfflineList = new ArrayList<>();
            for (int i = 1; i <= 1000000; i++) {
                AnalysisAssemblyOffline analysisAssemblyOffline = new AnalysisAssemblyOffline();
                analysisAssemblyOffline.setId((long) (i + 1003858));
                analysisAssemblyOffline.setIsDeleted(0);
                analysisAssemblyOffline.setProductionLine("LWHA-后副车架焊接线");
                analysisAssemblyOffline.setAssemblyNo("1ED505315D-SGR");
                analysisAssemblyOffline.setAssemblyBarcode("1ED 505 353 CN H1W 2BL 2023" + (i + 1003858));
                analysisAssemblyOffline.setCompletionTime(new Date());
                analysisAssemblyOffline.setStatus(1);
                analysisAssemblyOffline.setDataState("合格");
                analysisAssemblyOfflineList.add(analysisAssemblyOffline);
                if (i % 100000 == 0) {
                    this.saveBatch(analysisAssemblyOfflineList);
                    // 每 100000 条数据提交一次事务
                    analysisAssemblyOfflineList.clear();
                }
            }
            // 最后插入剩余的数据
            if (!CollectionUtil.isEmpty(analysisAssemblyOfflineList)) {
                this.saveBatch(analysisAssemblyOfflineList);
            }
            long spendTime = System.currentTimeMillis() - startTime;
            System.out.println("成功插入 100 万条数据,耗时：" + spendTime + "毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public AnalysisAssemblyOffline getTopOne(String productionSerialNo) {
        return baseMapper.selectTopOne(productionSerialNo);
    }

    @Override
    public void updateFinalState(String chaserCode, String beltline, String materialNo,String finalState) {
        baseMapper.updateFinalState(chaserCode,beltline,materialNo,finalState);
    }


    /**
     * 更新底盘号-fis使用
     * @param assemblyNo
     * @param assemblyFlowNo
     * @param chassisNo
     */
    @Override
    public void updateChassisNo(String assemblyNo, String assemblyFlowNo, String chassisNo) {
        baseMapper.updateChassisNo(assemblyNo,assemblyFlowNo,chassisNo);
    }

    @Override
    public void deleteParamAndComposition(String productionLine, String assemblyBarcode) {
        baseMapper.deleteParamAndComposition(productionLine,assemblyBarcode);
    }

    @Override
    public IPage<AssemblyNoVo> getAssemblyNoPage(IPage<AssemblyNoVo> page, AssemblyNoVo assemblyNoVo) {
        List<AssemblyNoVo> assemblyNoVoList=baseMapper.selectAssemblyNoPage(assemblyNoVo);
        // 需处理下分页
        int total = assemblyNoVoList.size();
        page.setTotal(total);
        if (total != 0) {
            int start = (int) (page.getSize() * (page.getCurrent() - 1));
            int end = (int) (page.getSize() * page.getCurrent());
            if (end > total) {
                end = total;
            }
            page.setRecords(assemblyNoVoList.subList(start, end));
        }
        return page;
    }

    @Override
    public IPage<AssemblyNoVo> getAssemblyBarcodePage(IPage<AssemblyNoVo> page, AssemblyNoVo assemblyNoVo) {
        return page.setRecords(baseMapper.selectAssemblyBarcodePage(page, assemblyNoVo));
    }

    @Override
    public List<AssemblyNoVo> getAssemblyNoByList(List<String> assList) {
        return baseMapper.selectAssemblyNoByList(assList);
    }
}
